package Model;

import Entity.Account;
import Manager.AccountManager;
import Manager.Manager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CONG
 */
public class AccountTableModel extends AbstractTableModel {

    private AccountManager am;
    private Manager manager;
    public AccountTableModel(Manager manager) {
        this.manager=manager;
    }

    @Override
    public int getRowCount() {
        return manager.getAccountManager().getSize();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Account account = manager.getAccountManager().getAccount(rowIndex);
        switch (columnIndex) {
            case 0:
                return account.getResgisterNo();
            case 1:
                return account.getName();
            case 2:
                SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy");
                String registerDate=dateFormat.format(account.getRegisterDate());
                return registerDate;
            case 3:
                return account.getPhone();
            case 4:
                return account.getAddress();
            case 5:
                return account.getEmail();
            case 6:
                return account.getOrganization();
            case 7:
                return account.getSalary();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Register Date";
            default:return null;
        }
    }
    
    public void addAccount(Account acc){
        manager.getAccountManager().addAccount(acc);
        fireTableDataChanged();
    }
    public void updateAccount(Account acc){
        manager.getAccountManager().updateAccount(acc);
        fireTableDataChanged();
    }
    public void removeAccount(int index){
        manager.getAccountManager().removeAccount(index);
        fireTableDataChanged();
    }
    public int getSize(){
        return manager.getAccountManager().getSize();
    }
    public Account getAccount(int index){
        return manager.getAccountManager().getAccount(index);
    }
    public void getAccountList(ArrayList<Account> l){
        manager.getAccountManager().getAccountList(l);
        fireTableDataChanged();
    }
}
